package aria.my12306;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends AppCompatActivity {
    private Button login;
    private TextView tvLostPass;
    private EditText edtUsername,edtPassword;
    private CheckBox autLogin=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        login=(Button)findViewById(R.id.loginButton);
        edtUsername=(EditText)findViewById(R.id.edtUsername);
        edtPassword=(EditText)findViewById(R.id.edtPassword);
        autLogin=(CheckBox)findViewById(R.id.atoLogin);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent i=new Intent();
                i.setClass(LoginActivity.this,MainActivity.class);
                startActivity(i);*/
                /*Log.d("my12306","aaa");*/
               /* Toast.makeText(getApplicationContext(),"my12306,
               登录",Toast.LENGTH_SHORT).show();*/

                if(TextUtils.isEmpty(edtUsername.getText().toString())){
                    edtUsername.setError("请输入用户名");
                    edtUsername.requestFocus();

                }
                else if (TextUtils.isEmpty(edtPassword.getText().toString())){
                    edtPassword.setError("请输入密码");
                    edtPassword.requestFocus();
                }else {
                    if(autLogin.isChecked()){
                        SharedPreferences pref=getSharedPreferences(
                                "user", Context.MODE_PRIVATE
                        );
                        SharedPreferences.Editor editor=pref.edit();
                        editor.putString("username",edtUsername.getText().toString());
                        editor.putString("password",edtPassword.getText().toString());
                        editor.commit();
                    }else {
                        SharedPreferences pref=getSharedPreferences("user",
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor=pref.edit();
                        editor.remove("username");
                        editor.remove("password");
                        editor.commit();
                    }
                    Intent i=new Intent();
                    i.setClass(LoginActivity.this,MainActivity.class);
                    startActivity(i);

                }
            }
        });
        tvLostPass=(TextView)findViewById(R.id.lostPassword);
        tvLostPass.setText(Html.fromHtml("<a href=" +
                "\"http://www.12306.cn/lostPassword\">忘记密码?</a>"));
        tvLostPass.setMovementMethod(LinkMovementMethod.getInstance());

    }
}
